package com.qys.livesMall.shortVideo.controller.sys;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.shortVideo.model.dto.app.EditMusicLibraryDTO;
import com.qys.livesMall.shortVideo.model.dto.app.QueryMusicLibraryDTO;
import com.qys.livesMall.shortVideo.model.entity.MusicLibrary;
import com.qys.livesMall.shortVideo.model.vo.app.MusicLibraryVO;
import com.qys.livesMall.shortVideo.service.MusicLibraryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 音乐库信息表(MusicLibrary)表控制层
 *
 * @author makejava
 * @since 2023-09-05 16:08:50
 */
@RestController
@RequestMapping("/sys/musicLibrary")
@Tag(name = "sys-音乐库管理", description = "App-音乐库管理")
public class SysMusicLibraryController {
    /**
     * 服务对象
     */
    @Resource
    private MusicLibraryService musicLibraryService;

    /**
     * 分页查询所有数据
     *
     * @param dto 查询实体
     * @return 所有数据
     */
    @PostMapping("/selectAll")
    @Operation(summary = "分页查询音乐库")
    public R<IPage<MusicLibraryVO>> selectAll(@RequestBody QueryMusicLibraryDTO dto) {
        return R.ok(musicLibraryService.selectAll(dto));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据id查询音乐")
    public R<MusicLibraryVO> selectOne(@PathVariable Serializable id) {
        return R.ok(musicLibraryService.selectOne(id));
    }

    /**
     * 新增/编辑数据
     *
     * @param dto 实体对象
     * @return 新增结果
     */
    @PostMapping("/edit")
    @Operation(summary = "新增/编辑音乐")
    public R<?> edit(@RequestBody EditMusicLibraryDTO dto) {
        MusicLibrary musicLibrary = new MusicLibrary();
        BeanUtil.copyProperties(dto, musicLibrary);
        if(StringUtils.isNotBlank(dto.getReleaseDate())){
            musicLibrary.setReleaseDate(LocalDate.parse(dto.getReleaseDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        }
        musicLibrary.setCreateBy(SecurityUtils.getUsername());
        musicLibraryService.saveOrUpdate(musicLibrary);
        return R.ok();
    }


    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @PostMapping("/del")
    @Operation(summary = "删除音乐")
    public R<?> delete(@RequestBody List<Long> idList) {
        musicLibraryService.removeByIds(idList);
        return R.ok();
    }
}

